package com.foreveross.taskservice.application.impl.task.statsdetail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.foreveross.taskservice.application.channel.IGradChannelApplication;
import com.foreveross.taskservice.application.impl.BaseJpaApplicationImpl;
import com.foreveross.taskservice.application.statistics.EngineStatisticsHistoryApplication;
import com.foreveross.taskservice.application.task.statsdetail.ICrawlStatsApplication;
import com.foreveross.taskservice.application.task.statsdetail.vo.CrawlStatsVO;
import com.foreveross.taskservice.common.CacheDataSpace;
import com.foreveross.taskservice.common.TaskStatusEnum;
import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.foreveross.taskservice.taskcache.ITaskCacheQuery;
import com.google.common.collect.Lists;
@Service
public class CrawlStatsApplicationImpl extends BaseJpaApplicationImpl implements ICrawlStatsApplication{
	@Resource(name="gradChannelApplicationImpl")
	private IGradChannelApplication gradChannelApplication;
	@Resource(name="redisTaskCacheQueryImpl")
	private ITaskCacheQuery taskCacheQuery;
//	@Resource(name ="engineStatisticsHistoryApplication")
	@Inject
	private EngineStatisticsHistoryApplication statisticsHistoryApplication;
	
	
	private String flag = "1";
	private String beginTime="";//查询范围-开始时间
	private String endTime="";//查询范围-结束时间
	@Override
	public List<CrawlStatsVO> getAllChannelCrawlSuccessStats()throws Exception {
		List<GradChannelEntity> channels=gradChannelApplication.getEnableChannel();
		long[] ids=new long[channels.size()];
		for(int i=0;i<ids.length;i++){
			ids[i]=channels.get(i).getId();
		}
		CacheDataSpace cd=CacheDataSpace.AirFreightSpace();
		//当前数据来源：redis
		Map<Long,Map<TaskStatusEnum,Long>> map = taskCacheQuery.queryTaskSize(cd, ids, TaskStatusEnum.values());
		long[] allByteLength=taskCacheQuery.queryChannelAllByteLength(cd, ids);
		long[] allCrawlCount=taskCacheQuery.queryChannelAllCrawlCounts(cd, ids);
		List<CrawlStatsVO> vos=Lists.newArrayList();
		for(int i=0;i<channels.size();i++){
			GradChannelEntity c = channels.get(i);
			if(map.containsKey(c.getId())){
				Map<TaskStatusEnum,Long> m=map.get(c.getId());
				int busiFailNum = 0, busiSuccessNum = 0;
				for(TaskStatusEnum en : m.keySet()){ //统计业务定义的状态类型数量
					if(en.getStatus()<= TaskStatusEnum.TASK_BUSI_FAIL.getStatus()) busiFailNum += m.get(en);
					if(en.getStatus()>= TaskStatusEnum.TASK_BUSI_SUCCESS.getStatus()) busiSuccessNum += m.get(en);
				}
				vos.add(new CrawlStatsVO(cd.getDate(), c.getId(), c.getName(),getHbaseKey(c.getId(),channels),
						 m.get(TaskStatusEnum.TASH_PAUSE),
						m.get(TaskStatusEnum.TASK_OTHER_FAIL), m.get(TaskStatusEnum.TASK_PARSE_FAIL),
						m.get(TaskStatusEnum.TASK_FETCH_FAIL), m.get(TaskStatusEnum.TASK_WATI),
						m.get(TaskStatusEnum.TASK_DOING), busiFailNum,
						busiSuccessNum,
						m.get(TaskStatusEnum.TASK_SUCCESS), 
						allByteLength[i], allCrawlCount[i]));
			}
		}
		
		return vos;
	}
	
	
	//hbaseKey
	public static String getHbaseKey(Long id,List<GradChannelEntity> channels){
		String hbaseKey=null;
		if(channels.size()>0){
			for(GradChannelEntity e:channels){
				if(id==e.getId()){
					hbaseKey=e.getHbaseKey();
					break;
				}
			}
		}
		return hbaseKey;
	}
	

	@Override
	public List<CrawlStatsVO> showAllChannelCrawlSuccessStats()throws Exception {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		if(flag.equals("1")){
			endTime=sdf.format(date);
			date=org.apache.commons.lang3.time.DateUtils.addDays(date,-1);
			beginTime=sdf.format(date);
		}else {
			endTime=sdf.format(date);
			date=org.apache.commons.lang3.time.DateUtils.addDays(date,-7);
			beginTime=sdf.format(date);
		}
		//当天抓取数据数据来源：redis
		List<CrawlStatsVO> Vo=Lists.newArrayList();
		Vo=getAllChannelCrawlSuccessStats();
		//历史数据(第一层)来源于mysql(持久化后的数据)
		List<CrawlStatsVO> voh=Lists.newArrayList();
		//查询mysql	
//		voh=statisticsHistoryApplication.findVoByDate(beginTime.trim(),endTime.trim());//查询今天之前(历史)的数据

		if(voh.size()>0){
			Vo.addAll(voh);
		}
		
		return Vo;
	}

}
